package com.pacy.pacybaseproject;

import androidx.fragment.app.Fragment;

import android.Manifest;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.pacy.pacybaseproject.activitys.TravelActivity;
import com.pacy.pacybaseproject.base.BaseXActivity;
import com.pacy.pacybaseproject.databinding.ActivityMainBinding;
import com.pacy.pacybaseproject.events.MainTabEvent;
import com.pacy.pacybaseproject.fragments.HomeFragment;
import com.pacy.pacybaseproject.fragments.IntegralFragment;
import com.pacy.pacybaseproject.fragments.MineFragment;
import com.pacy.pacybaseproject.fragments.RewardFragment;
import com.pacy.pacybaseproject.viewmodels.MainViewModel;
import com.tbruyelle.rxpermissions2.RxPermissions;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.views.bottombar.BottomBarTab;
import me.goldze.mvvmhabit.views.bottombar.BottomCheckBar;

public class MainActivity extends BaseXActivity<ActivityMainBinding, MainViewModel> {
    private List<Fragment> mFragments = new ArrayList<>();
    private long exitTime = -1L;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        EventBus.getDefault().register(this);
        initFragments();
        initBottomCheckBar();

//        KLog.i("我的设备id="+ JPushInterface.getRegistrationID(this));

        binding.middleIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(TravelActivity.class);
            }
        });
    }



    //初始化fragment
    private void initFragments() {
        mFragments.add(HomeFragment.newInstance("首页"));
        mFragments.add(RewardFragment.newInstance("服务"));
        mFragments.add(IntegralFragment.newInstance("积分"));
//        mFragments.add(TravelFragment.newInstance("出行"));
        mFragments.add(MineFragment.newInstance("我的"));
    }

    /**
     * 两套图片  icon切换
     */
    private void initBottomCheckBar() {
        binding.bottomBar.setFragments(mFragments)
                .init(getSupportFragmentManager(), R.id.fly)
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.home_tab_bg, "首页"))
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.reward_tab_bg, "服务"))
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.msg_tab_bg, "积分"))
//                .addItem(new BottomBarTab(MainActivity.this, R.drawable.travel_tab_bg, "出行"))
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.my_tab_bg, "我的"))
                .addMiddleIcon(R.drawable.cjwt_icon)
                .defaultIndex(0);
        binding.bottomBar.setOnTabSelectedListener(new BottomCheckBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                switch (position) {
                    case 1:
                    case 2:
                    case 3:
//                        ImmersionBar.with(MainActivity.this).fitsSystemWindows(false).statusBarColor(R.color.transparent).init();
                        ImmersionBar.with(MainActivity.this).statusBarDarkFont(true).init();
                        break;
                    default:
                        ImmersionBar.with(MainActivity.this).statusBarDarkFont(false).init();
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                ToastUtils.showShort("再次点击---"+position);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtils.showShort("再按一次返回退出");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    /**
     * 刷新数据
     *
     * @param event
     */
    @Subscribe()
    public void onEvent(MainTabEvent event) {
        binding.bottomBar.setCurrentItem(event.getCurrTab());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}